﻿using System.Collections.Generic;

using SharpArch.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Data.NHibernate;
using SharpArch.Testing.NUnit;
using SharpArch.Testing.NUnit.NHibernate;

using NUnit.Framework;

using Quiziqal.Domain;
using Quiziqal.Persistence;

namespace Quiziqal.Specifications.Persistence
{
    [TestFixture]
    public class when_saving_and_retreiving_a_new_user : PersistenceTestBase
    {
        IRepository<User> repository = new UserRepository();
        const string USERNAME = "Arthur_Dent";
        User toDb;
        User fromDb;

        protected override void EstablishContext()
        {
            toDb = new User(USERNAME);
        }

        protected override void Act()
        {
            repository.SaveOrUpdate(toDb);
            FlushSessionAndEvict(toDb);
            fromDb = repository.Get(toDb.ID);
        }

        [Test]
        public void retreived_user_is_not_null()
        {
            fromDb.ShouldNotBeNull();
        }

        [Test]
        public void retreived_user_should_not_be_same_as_saved_user()
        {
            fromDb.ShouldNotBeTheSameAs(toDb);
        }

        [Test]
        public void retrieved_user_should_be_equal_to_saved_user()
        {
            fromDb.ShouldEqual(toDb);
        }
    }

    [TestFixture]
    public class when_deleting_a_user : PersistenceTestBase
    {
        IRepository<User> repository = new UserRepository();
        const string USERNAME = "Arthur_Dent";
        User user;

        protected override void EstablishContext()
        {
            user = CreatePersistedUser(USERNAME);
        }

        protected override void Act()
        {
            repository.Delete(user);
        }

        [Test]
        public void retreived_user_is_null()
        {
            var fromDb = repository.Get(user.ID);
            fromDb.ShouldBeNull();
        }
    }
}
